package com.augurit.gzcgwsp.webservice;

import gzcgwsp.IcCard;

import java.text.ParseException;

import org.apache.log4j.Logger;

import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.common.util.WSClient;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.ljzyz.web.form.YwYnBusinfoForm;

/**
 * 车辆ic卡调用类
 * @version 1.0 
 * @since 2011-5-11
 */
public class IccardWebservice {
	private static Logger log=Logger.getLogger(WSClient.class);
   
	/**
	 * 读卡
	 * @return
	 * @throws ParseException 
	 * @throws Exception
	 */
    public static YwYnBusinfoForm  readInfo() throws ParseException {
	    YwYnBusinfoForm form=null;
	    String cardData = IcCard.readCard();
	    //cardData="AN076;粤A82387;广州三元石方运输有限公司;LZ5N2CD3XAB017031;1610F152309;2013030120130930";
		if(StringUtils.isNotBlank(cardData)){
			   form=new YwYnBusinfoForm();
			   String[] datas=cardData.split(";");
			    String bzh=datas[0];
			    String cphm=datas[1];
			    String gsmc=datas[2];
			    String cjh=datas[3];
			    String fdjh=datas[4];
			    String yxrq=datas[5];
			    if(yxrq.length()>=16){
			    	String startdate=yxrq.substring(0,8);
			    	String enddate=yxrq.substring(8,16);
			    	form.setYxksrq(DateUtilsEX.dateFormate(startdate, "yyyyMMdd"));
			 	    form.setYxjsrq(DateUtilsEX.dateFormate(enddate, "yyyyMMdd"));
			    }
			    form.setBzh(bzh);
			    form.setClbh(bzh);
			    form.setCphm(cphm);
			    form.setGsmc(gsmc);
			    form.setCjh(cjh);
			    form.setFdjh(fdjh);
		}
	 
        return form;
	}
    
    /**
     * 写卡
     * @return
     * @throws Exception
     */
    public static boolean   writeInfo(String content) {
	    boolean flag=true;
	    long val=IcCard.writeCard(content);
	    String reval=String.valueOf(val);
	   if(!"0".equals(reval)){
		   flag=false;
	   }
        return flag;
	}
    
    /**
     * 修改有效时间
     * @return
     * @throws Exception
     */
    public static boolean   updateCard(String valiDate) {
	    boolean flag=true;
	    long val=IcCard.updateCardDate(valiDate);
	    String reval=String.valueOf(val);
		   if(!"0".equals(reval)){
			   flag=false;
		}
        return flag;
	}
    
    
    /**
     * 是否发声成功
     * port 端口
     * type表示发声类型0发短声，1发长声。
     */
    public static boolean isSound(int port,int type){
    	
    	return true;
    }
    

  
   /**
    * 打开串口
    * @param port表示串口号
    * @return
    */
   public static boolean  openDev(int port){
	   boolean flag=true;
	   long val=IcCard.openDev(1);
	   String reval=String.valueOf(val);
	   if(!"0".equals(reval)){
		   flag=false;
	   }
	   return flag;
    }
    
    /**
     * 查找ic卡
     * @param tagType
     * @return ic卡的标识
     */
   public static  String findIccard(int ptype){
	   
	   return null;
   }
   
   public static boolean  delCard() throws Exception{
   	
	   return true;
    }
    

    /**
     * 关闭串口
     * @return
     */
   public static boolean closeDev(){
	   boolean flag=true;
	  try {
		IcCard.closeDev();
	} catch (RuntimeException e) {
		flag=false;
		e.printStackTrace();
	}
	   return flag;
    }
    
    


}
